<script setup name='login'>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import {OA_login} from '@/apis/user'
import { UseInfo_Store } from '@/stores/index'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const form = ref()
const userinfo_pinia = UseInfo_Store()
const formModel = ref({
    username: '',
    password: ''                      
})
const rules = {
    username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },                     
    { min: 2, max: 20, message: '用户名长度必须是2-10位字符', trigger: 'blur' }
    ],
    password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\S{6,20}$/,message: '密码必须是6-15位的非空字符' , trigger: 'blur' }
    ]
}

const is_login = async() => {
    await form.value.validate()
    let user_info = await OA_login(formModel.value.username, formModel.value.password)
    console.log('后端返回的值',user_info)
    userinfo_pinia.set_token(user_info.token)
    userinfo_pinia.set_use_info(user_info.username, user_info.avatar)
    router.push('/')
    ElMessage.success('登陆成功')
}


</script>

<template>
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>                                      
        <el-col :span="6" :offset="3" class="form">
            <div class="title">
                <div>内部系统登录</div>
            </div>                                  
            <el-form :model="formModel" :rules="rules" size="large" ref="form" autocomplete="off" >    
            <el-form-item prop="username">
                <el-input v-model="formModel.username" :prefix-icon="User"  placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="formModel.password" :prefix-icon="Lock" name="password" type="password"  placeholder="请输入密码"></el-input>
            </el-form-item>

            <el-form-item>
            <el-button class="button" type="primary" @click="is_login" >登录</el-button>
            </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<style lang="scss" scoped>
.login-page {
    height: 100vh;                                                  
    background-color: #fff;
}
.bg {
    background: 
     url('@/assets/back.jpeg') no-repeat center /cover;                      
}
.form {
    display: flex;                                                           
    flex-direction: column;                                                  
    justify-content: center;                                                 
    user-select: none;     
    .title{
        font-size: 200%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 80px;
    }                                                  
    .button {
        width: 100%;
    }
    .flex {
        width: 100%;
        display: flex;
        justify-content: space-between;
    }
}
</style>
